﻿#include "pch.h"
#include "PropertyData.h"
#include <stdlib.h>
#include <string.h>

CTYW* pCtyw = NULL;
PipeMaterialProperties* pPmp = NULL;
Insul* pInsul = NULL;

CTYW* SetCurrentCtyw(char* city)
{

    pCtyw = (CTYW*)malloc(sizeof(CTYW));
	CTYW* pCtywTemp = (CTYW*)malloc(sizeof(CTYW) * 80);
    int row = getPropertyData("dat/ctyw.dat", pCtywTemp, 0, 0);
    if (pCtywTemp != NULL)
    {
        for (int i = 0; i < row; i++)
        {
            if (strcmp((pCtywTemp + i)->city, city) == 0)
            {
				getPropertyData("dat/ctyw.dat", pCtyw, i, 1);
            }
        }
    }
	free(pCtywTemp);
    return pCtyw;
}


PipeMaterialProperties* SetCurrentPmp(unsigned int id)
{
	pPmp = (PipeMaterialProperties*)malloc(sizeof(PipeMaterialProperties));
	getPropertyData("dat/material.dat", pPmp, id, 1);
	return pPmp;
}

Insul* SetCurrentInsul(unsigned int id, double tm)
{
    pInsul = (Insul*)malloc(sizeof Insul);
    getPropertyData("dat/Insulation.dat", pInsul, id, 1);

    char preNameBuf[40] = "\0", preRemarkBuf[20] = "\0";
    unsigned int preDensity_min = 100000, preDensity_max = 100000;
    if (pInsul != NULL)
    {
        strcpy_s(preNameBuf, sizeof preNameBuf, pInsul->name);
        strcpy_s(preRemarkBuf, sizeof preRemarkBuf, pInsul->remark);
        preDensity_max = pInsul->density_max;
        preDensity_min = pInsul->density_min;

        if (tm >= pInsul->tm_min && tm <= pInsul->tm_max)
        {
            return pInsul;
        }

        if (tm < pInsul->tm_min && id - 1 >= 0)
        {
            getPropertyData("dat/Insulation.dat", pInsul, id - 1, 1);
        }
        else if (tm > pInsul->tm_max && id + 1 < INSUL_NUM_MAX)
        {
            getPropertyData("dat/Insulation.dat", pInsul, id + 1, 1);
        }
        else
        {
            return NULL;
        }

        if (strcmp(preNameBuf, pInsul->name) == 0 && strcmp(preRemarkBuf, pInsul->remark) == 0 &&
            preDensity_max == pInsul->density_max && preDensity_min == pInsul->density_min)
        {
            return pInsul;
        }
        else
        {
            return NULL;
        }
    }

	return NULL;
}

char* LPWSTRToChar(wchar_t* lpwszBuf)
{
	int nLen = WideCharToMultiByte(CP_OEMCP, NULL, lpwszBuf, -1, NULL, 0, NULL, FALSE);
	char *chBuf = new char[nLen];
	WideCharToMultiByte(CP_OEMCP, NULL, lpwszBuf, -1, chBuf, nLen, NULL, FALSE);
	return chBuf;
}

char* GetExePath()
{
	wchar_t szBuf[MAX_PATH];
	HMODULE module = GetModuleHandle(0);
	GetModuleFileNameW(module, szBuf, MAX_PATH);
	char *chBuf = LPWSTRToChar(szBuf);
	char *ret = strrchr(chBuf, '\\');
	*ret = '\0';
	return chBuf;
}

char* GetFullFileName(const char* pFileName)
{
	char* pFullName = new char[MAX_PATH];
	strcpy_s(pFullName, strlen(GetExePath()) + 1, GetExePath());
	strcat_s(pFullName, strlen(pFullName) + 2, "\\");
	strcat_s(pFullName, strlen(pFullName) + strlen(pFileName) + 1, pFileName);
	return pFullName;
}

const char componentList[121][5][255] = {
	{ "acetone", "acetone.fld", "67-64-1", "propanone", "178.5-550 K, 700 MPa" },
	{ "ammonia", "ammonia.fld", "7664-41-7", "ammonia", "195.495-700 K, 1000 MPa" },
	{ "argon", "argon.fld", "7440-37-1", "argon", "83.8058-2000 K, 1000 MPa" },
	{ "benzene", "benzene.fld", "71-43-2", "benzene", "278.674-725 K, 500 MPa" },
	{ "butane", "butane.fld", "106-97-8", "n-butane", "134.895-575 K, 200 MPa" },
	{ "butene", "1butene.fld", "106-98-9", "1-butene", "87.8-525 K, 70 MPa" },
	{ "carbon dioxide", "co2.fld", "124-38-9", "carbon dioxide", "216.592-2000 K, 800 MPa" },
	{ "carbon monoxide", "co.fld", "630-08-0", "carbon monoxide", "68.16-500 K, 100 MPa" },
	{ "carbonyl sulfide", "cos.fld", "463-58-1", "carbon oxide sulfide", "134.3-650 K, 50 MPa" },
	{ "cis-butene", "c2butene.fld", "590-18-1", "cis-2-butene", "134.3-525 K, 50 MPa" },
	{ "cyclohexane", "cyclohex.fld", "110-82-7", "cyclohexane", "279.47-700 K, 250 MPa" },
	{ "cyclopentane", "cyclopen.fld", "287-92-3", "cyclopentane", "179.7-550 K, 250 MPa" },
	{ "cyclopropane", "cyclopro.fld", "75-19-4", "cyclopropane", "273-473 K, 28 MPa" },
	{ "D4", "d4.fld", "556-67-2", "octamethylcyclotetrasiloxane", "290.25-673 K, 30 MPa" },
	{ "D5", "d5.fld", "541-02-6", "decamethylcyclopentasiloxane", "300-673 K, 30 MPa" },
	{ "D6", "d6.fld", "540-97-6", "dodecamethylcyclohexasiloxane", "270.2-673 K, 30 MPa" },
	{ "decane", "decane.fld", "124-18-5", "decane", "243.5-675 K, 800 MPa" },
	{ "diethyl ether", "dee.fld", "60-29-7", "diethyl ether", "270-500 K, 40 MPa" },
	{ "deuterium", "d2.fld", "7782-39-0", "deuterium", "18.724-600 K, 2000 MPa" },
	{ "dimethyl carbonate", "dmc.fld", "616-38-6", "dimethyl ester carbonic acid", "277.06-600 K, 60 MPa" },
	{ "dimethylether", "dme.fld", "115-10-6", "methoxymethane", "131.66-525 K, 40 MPa" },
	{ "dodecane", "c12.fld", "112-40-3", "dodecane", "263.6-700 K, 700 MPa" },
	{ "ethylbenzene", "ebenzene.fld ", "100-41-4", "phenylethane", "178.2-700 K, 60 MPa" },
	{ "ethane", "ethane.fld", "74-84-0", "ethane", "90.368-675 K, 900 MPa" },
	{ "ethanol", "ethanol.fld", "64-17-5", "ethyl alcohol", "159-650 K, 280 MPa" },
	{ "ethylene", "ethylene.fld", "74-85-1", "ethene", "103.986-450 K, 300 MPa" },
	{ "fluorine", "fluorine.fld", "7782-41-4", "fluorine", "53.4811-300 K, 20 MPa" },
	{ "hydrogen chloride", "hcl.fld", "7647-01-0", "hydrogen chloride", "155-330 K, 20 MPa" },
	{ "heavy water", "d2o.fld", "7789-20-0", "deuterium oxide", "276.97-800 K, 100 MPa" },
	{ "helium", "helium.fld", "7440-59-7", "helium-4", "2.1768-2000 K, 1000 MPa" },
	{ "heptane", "heptane.fld", "142-82-5", "heptane", "182.55-600 K, 100 MPa" },
	{ "hexane", "hexane.fld", "110-54-3", "hexane", "177.83-600 K, 100 MPa" },
	{ "hydrogen (normal)", "hydrogen.fld", "1333-74-0", "hydrogen (normal)", "13.957-1000 K, 2000 MPa" },
	{ "hydrogen sulfide", "h2s.fld", "7783-06-4", "hydrogen sulfide", "187.7-760 K, 170 MPa" },
	{ "isoctane", "ioctane.fld", "540-84-1", "2,2,4-trimethylpentane", "165.77-600 K, 1000 MPa" },
	{ "isobutane", "isobutan.fld", "75-28-5", "2-methylpropane", "113.73-575 K, 35 MPa" },
	{ "isobutene", "ibutene.fld", "115-11-7", "2-methyl-1-propene", "132.4-550 K, 50 MPa" },
	{ "isohexane", "ihexane.fld", "107-83-5", "2-methylpentane", "119.6-550 K, 1000 MPa" },
	{ "isopentane", "ipentane.fld", "78-78-4", "2-methylbutane", "112.65-500 K, 1000 MPa" },
	{ "krypton", "krypton.fld", "7439-90-9", "krypton", "115.775-750 K, 200 MPa" },
	{ "md2m", "md2m.fld", "141-62-8", "decamethyltetrasiloxane", "205.2-673 K, 30 MPa" },
	{ "md3m", "md3m.fld", "141-63-9", "dodecamethylpentasiloxane", "192-673 K, 30 MPa" },
	{ "md4m", "md4m.fld", "107-52-8", "tetradecamethylhexasiloxane", "300-673 K, 30 MPa" },
	{ "mdm", "mdm.fld", "107-51-7", "octamethyltrisiloxane", "187.2-673 K, 30 MPa" },
	{ "methane", "methane.fld", "74-82-8", "methane", "90.6941-625 K, 1000 MPa" },
	{ "methanol", "methanol.fld", "67-56-1", "methanol", "175.61-620 K, 800 MPa" },
	{ "methyl linoleate", "mlinolea.fld", "112-63-0", "methyl (Z,Z)-9,12-octadecadienoate", "238.1-1000 K, 50 MPa" },
	{ "methyl linolenate", "mlinolen.fld", "301-00-8", "methyl (Z,Z,Z)-9,12,15-octadecatrienoate", "218.65-1000 K, 50MPa" },
	{ "methyl oleate", "moleate.fld", "112-62-9", "methyl cis-9-octadecenoate", "253.47-1000 K, 50 MPa" },
	{ "methyl palmitate", "mpalmita.fld", "112-39-0", "methyl hexadecanoate", "302.71-1000 K, 50 MPa" },
	{ "methyl stearate", "mstearat.fld", "112-61-8", "methyl octadecanoate", "311.84-1000 K, 50 MPa" },
	{ "methylcyclohexane", "c1cc6.fld", "108-87-2", "methylcyclohexane", "146.7-600 K, 500 MPa" },
	{ "MM", "mm.fld", "107-46-0", "hexamethyldisiloxane", "273-673 K, 30 MPa" },
	{ "m-xylene", "mxylene.fld", "108-38-3", "1,3-dimethylbenzene", "225.3-700 K, 200 MPa" },
	{ "neon", "neon.fld", "7440-01-9", "neon", "24.556-700 K, 700 MPa" },
	{ "neopentane", "neopentn.fld", "463-82-1", "2,2-dimethylpropane", "256.6-550 K, 200 MPa" },
	{ "nitrogen", "nitrogen.fld", "7727-37-9", "nitrogen", "63.151-2000 K, 2200 MPa" },
	{ "nitrogen triflouride", "nf3.fld", "7783-54-2", "nitrogen trifluoride", "85-500 K, 50 MPa" },
	{ "nitrous oxide", "n2o.fld", "10024-97-2", "dinitrogen monoxide", "182.33-525 K, 50 MPa" },
	{ "nonane", "nonane.fld", "111-84-2", "nonane", "219.7-600 K, 800 MPa" },
	{ "Novec-649", "novec649.fld", "756-13-8", "Dodecafluoro-2-methylpentan-3-one", "165-500 K, 50 MPa" },
	{ "octane", "octane.fld", "111-65-9", "octane", "216.37-600 K, 100 MPa" },
	{ "orthohydrogen", "orthohyd.fld", "1333-74-0o", "orthohydrogen", "14.008-1000 K, 2000 MPa" },
	{ "oxygen", "oxygen.fld", "7782-44-7", "oxygen", "54.361-2000 K, 82 MPa" },
	{ "o-xylene", "oxylene.fld", "95-47-6", "1,2-dimethylbenzene", "247.985-700 K, 70 MPa" },
	{ "parahydrogen", "parahyd.fld", "1333-74-0p", "parahydrogen", "13.8033-1000 K, 2000 MPa" },
	{ "pentane", "pentane.fld", "109-66-0", "pentane", "143.47-600 K, 100 MPa" },
	{ "perfluorobutane", "c4f10.fld", "355-25-9", "decafluorobutane", "189-500 K, 30 MPa" },
	{ "perfluoropentane", "c5f12.fld", "678-26-2", "dodecafluoropentane", "148.363-500 K, 30 MPa" },
	{ "propane", "propane.fld", "74-98-6", "propane", "85.525-650 K, 1000 MPa" },
	{ "propylcyclohexane", "c3cc6.fld", "1678-92-8", "n-propylcyclohexane", "178.2-650 K, 50 MPa" },
	{ "propylene", "propylen.fld", "115-07-1", "propene", "87.953-575 K, 1000 MPa" },
	{ "propyne", "propyne.fld", "74-99-7", "propyne", "273-474 K, 32 MPa" },
	{ "p-xylene", "pxylene.fld", "106-42-3", "1,4-dimethylbenzene", "286.4-700 K, 200 MPa" },
	{ "R11", "r11.fld", "75-69-4", "trichlorofluoromethane", "162.68-625 K, 30 MPa" },
	{ "R113", "r113.fld", "76-13-1", "1,1,2-trichloro-1,2,2-trifluoroethane", "236.93-525 K, 200 MPa" },
	{ "R114", "r114.fld", "76-14-2", "1,2-dichloro-1,1,2,2-tetrafluoroethane", "273.15-507 K, 21 MPa" },
	{ "R115", "r115.fld", "76-15-3", "chloropentafluoroethane", "173.75-550 K, 60 MPa" },
	{ "R116", "r116.fld", "76-16-4", "hexafluoroethane", "173.1-425 K, 50 MPa" },
	{ "R12", "r12.fld", "75-71-8", "dichlorodifluoromethane", "116.099-525 K, 200 MPa" },
	{ "R1216", "r1216.fld", "116-15-4", "hexafluoropropylene", "117.654-400 K, 12 MPa" },
	{ "R123", "r123.fld", "306-83-2", "2,2-dichloro-1,1,1-trifluoroethane", "166-600 K, 40 MPa" },
	{ "R1233zd(E)", "r1233zd.fld", "102687-65-0", "1-chloro-3,3,3-trifluoroprop-1-ene", "195.15-550 K,100 MPa" },
	{ "R1234yf", "r1234yf.fld", "754-12-1", "2,3,3,3-tetrafluoroprop-1-ene", "220-410 K, 30 MPa" },
	{ "R1234ze(E)", "r1234ze.fld", "29118-24-9", "trans-1,3,3,3-tetrafluoropropene", "168.62-420 K, 20 MPa" },
	{ "R124", "r124.fld", "2837-89-0", "1-chloro-1,2,2,2-tetrafluoroethane", "120-470 K, 40 MPa" },
	{ "R125", "r125.fld", "354-33-6", "pentafluoroethane", "172.52-500 K, 60 MPa" },
	{ "R13", "r13.fld", "75-72-9", "chlorotrifluoromethane", "92-403 K, 35 MPa" },
	{ "R134a", "r134a.fld", "811-97-2", "1,1,1,2-tetrafluoroethane", "169.85-455 K, 70 MPa" },
	{ "R14", "r14.fld", "75-73-0", "tetrafluoromethane", "120-623 K, 51 MPa" },
	{ "R141b", "r141b.fld", "1717-00-6", "1,1-dichloro-1-fluoroethane", "169.68-500 K, 400 MPa" },
	{ "R142b", "r142b.fld", "75-68-3", "1-chloro-1,1-difluoroethane", "142.72-470 K, 60 MPa" },
	{ "R143a", "r143a.fld", "420-46-2", "1,1,1-trifluoroethane", "161.34-650 K, 100 MPa" },
	{ "R152a", "r152a.fld", "75-37-6", "1,1-difluoroethane", "154.56-500 K, 60 MPa" },
	{ "R161", "r161.fld", "353-36-6", "fluoroethane", "130-450 K, 5 MPa" },
	{ "R21", "r21.fld", "75-43-4", "dichlorofluoromethane", "200-473 K, 138 MPa" },
	{ "R218", "r218.fld", "76-19-7", "octafluoropropane", "125.45-440 K, 20 MPa" },
	{ "R22", "r22.fld", "75-45-6", "chlorodifluoromethane", "115.73-550 K, 60 MPa" },
	{ "R227ea", "r227ea.fld", "431-89-0", "1,1,1,2,3,3,3-heptafluoropropane", "146.35-475 K, 60 MPa" },
	{ "R23", "r23.fld", "75-46-7", "trifluoromethane", "118.02-475 K, 120 MPa" },
	{ "R236ea", "r236ea.fld", "431-63-0", "1,1,1,2,3,3-hexafluoropropane", "240-412 K, 6 MPa" },
	{ "R236fa", "r236fa.fld", "690-39-1", "1,1,1,3,3,3-hexafluoropropane", "179.6-400 K, 70 MPa" },
	{ "R245ca", "r245ca.fld", "679-86-7", "1,1,2,2,3-pentafluoropropane", "191.5-450 K, 10 MPa" },
	{ "R245fa", "r245fa.fld", "460-73-1", "1,1,1,3,3-pentafluoropropane", "171.05-440 K, 200 MPa" },
	{ "R32", "r32.fld", "75-10-5", "difluoromethane", "136.34-435 K, 70 MPa" },
	{ "R365mfc", "r365mfc.fld", "406-58-6", "1,1,1,3,3-pentafluorobutane", "239-500 K, 35 MPa" },
	{ "R40", "r40.fld", "74-87-3", "methyl chloride", "230-630 K, 100 MPa" },
	{ "R41", "r41.fld", "593-53-3", "fluoromethane", "129.82-425 K, 70 MPa" },
	{ "RC318", "rc318.fld", "115-25-3", "octafluorocyclobutane", "233.35-623 K, 60 MPa" },
	{ "RE143a", "re143a.fld", "421-14-7", "methyl trifluoromethyl ether", "240-420 K, 7.2 MPa" },
	{ "RE245cb2", "re245cb2.fld", "22410-44-2", "methyl-pentafluoroethyl-ether", "250-500 K, 20 MPa" },
	{ "RE245fa2", "re245fa2.fld", "1885-48-9", "2,2,2-trifluoroethyl-difluoromethyl-ether ", "250-500 K, 400 MPa" },
	{ "RE347mcc", "re347mcc.fld", "375-03-1", "methyl-heptafluoropropyl-ether", "250-500 K, 20 MPa" },
	{ "sulfur dioxide", "so2.fld", "7446-09-5", "sulfur dioxide", "197.7-525 K, 35 MPa" },
	{ "sulfur hexafluoride", "sf6.fld", "2551-62-4", "sulfur hexafluoride", "223.555-625 K, 150 MPa" },
	{ "toluene", "toluene.fld", "108-88-3", "methylbenzene", "178-700 K, 500 MPa" },
	{ "trans-butene", "t2butene.fld", "624-64-6", "trans-2-butene", "167.6-525 K, 50 MPa" },
	{ "trifluoroiodomethane", "cf3i.fld", "2314-97-8", "trifluoroiodomethane", "120-420 K, 20 MPa" },
	{ "undecane", "c11.fld", "1120-21-4", "undecane", "247.541-700 K, 500 MPa" },
	{ "water", "water.fld", "7732-18-5", "water", "273.16-2000 K, 1000 MPa" },
	{ "xenon", "xenon.fld", "7440-63-3", "xenon", "161.405-750 K, 700 MPa" }
};